package com.skydroid.skydroiddoublevideo

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import tv.danmaku.ijk.media.player.IMediaPlayer
import java.io.IOException

class DoubleVideoActivity: AppCompatActivity() {
    private val path1 = "rtsp://192.168.144.108:8000/165506"
    private val path2 = "rtsp://192.168.144.108:8000/375897"
    private var video1: TextureVideoView? = null
    private var video2: TextureVideoView? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_double_video)
        initView()
        init()
    }

    companion object{
        fun start(context: Context){
            context.startActivity(
                Intent(context,
                    DoubleVideoActivity::class.java)
            )
        }
    }

    private fun initView(){

        video1 = findViewById(R.id.video1)
//        video1?.setMediacodec(0)

        video1?.setPath(path1)
//        videoView?.setPath("udp://@:13551")
        try {
            video1?.load()
        } catch (e: IOException) {
            Toast.makeText(this, "播放失败", Toast.LENGTH_SHORT)
            e.printStackTrace()
        }


        video2 = findViewById(R.id.video2)
        video2?.setPath(path2)
        try {
            video2?.load()
        } catch (e: IOException) {
            Toast.makeText(this, "播放失败", Toast.LENGTH_SHORT)
            e.printStackTrace()
        }
    }

    private fun init(){
        video1?.setVideoListener(object : VideoListener{
            override fun onSeekComplete(p0: IMediaPlayer?) {
            }

            override fun onInfo(p0: IMediaPlayer?, p1: Int, p2: Int): Boolean {
                return false
            }

            override fun onVideoSizeChanged(p0: IMediaPlayer?, p1: Int, p2: Int, p3: Int, p4: Int) {
            }

            override fun onBufferingUpdate(p0: IMediaPlayer?, p1: Int) {
            }

            override fun onPrepared(p0: IMediaPlayer?) {
                video1?.start()
            }

            override fun onCompletion(p0: IMediaPlayer?) {
            }

            override fun onError(p0: IMediaPlayer?, p1: Int, p2: Int): Boolean {
                if(p1 == -10000 && p2 == 0){
                    video1?.load()
                }
                return false
            }
        })

        video2?.setVideoListener(object : VideoListener{
            override fun onSeekComplete(p0: IMediaPlayer?) {

            }

            override fun onInfo(p0: IMediaPlayer?, p1: Int, p2: Int): Boolean {
                return false
            }

            override fun onVideoSizeChanged(p0: IMediaPlayer?, p1: Int, p2: Int, p3: Int, p4: Int) {

            }

            override fun onBufferingUpdate(p0: IMediaPlayer?, p1: Int) {

            }

            override fun onPrepared(p0: IMediaPlayer?) {
                video2?.start()
            }

            override fun onCompletion(p0: IMediaPlayer?) {

            }

            override fun onError(p0: IMediaPlayer?, p1: Int, p2: Int): Boolean {
                if(p1 == -10000 && p2 == 0){
                    video2?.load()
                }
                return false
            }
        })
    }

    override fun onDestroy() {
        super.onDestroy()
        video1?.stop()
        video1?.release()
        video2?.stop()
        video2?.release()
    }
}